---
--类名: StoryModeView.lua
--创建日期: 2015-5-8
--创建人: 陈小虎
--功能描述: 故事剧情模式视图
--修改内容：
--修改日期:   
--修改人:     
--修改备注: 
--

local StoryModeView = class("StoryModeView",function()
    return display.newLayer("StoryModeView")
end)

function StoryModeView:ctor(controller)
    self.mController = controller
   
    self.mMaskWidth = display.width
    self.mMaskHeight = display.height / 6 / 4 * 3
    local InsetsRect = cc.rect(5,5,5,5)
    self.mUpMask = ccui.Scale9Sprite:create(InsetsRect,Res.Public_Black_Img)--cc.LayerColor:create(cc.c4b(0,0,0,255),self.mMaskWidth,self.mMaskHeight)
    self.mUpMask:setContentSize(cc.size(self.mMaskWidth,self.mMaskHeight))
    self.mUpMask:setAnchorPoint(cc.p(0,0))
    self.mUpMask:setPosition(cc.p(0,display.height))
    self:addChild(self.mUpMask)
    
    self.mBottomMask = ccui.Scale9Sprite:create(InsetsRect,Res.Public_Black_Img)--cc.LayerColor:create(cc.c4b(0,0,0,255),self.mMaskWidth,self.mMaskHeight)
    self.mBottomMask:setContentSize(cc.size(self.mMaskWidth,self.mMaskHeight))
    self.mBottomMask:setAnchorPoint(cc.p(0,0))
    self.mBottomMask:setPosition(cc.p(0,0 - self.mMaskHeight))
    self:addChild(self.mBottomMask)
    
    self.mUpMask:setVisible(false)
    self.mBottomMask:setVisible(false)

    self:setNodeEventEnabled(true)
    
    -- --点击任意处继续标签
    -- self.mHint = display.newTTFLabel({
    --     text = "点击任意处继续",
    --     font = Res.font,
    --     size = 30,
    --     align = cc.TEXT_ALIGNMENT_CENTER -- 文字内部居中对齐
    --     })
    -- --self:addChild(self.mHint)
    -- self.mHint:setPosition(cc.p(self.mMaskWidth / 2,self.mMaskHeight / 2))
    -- --self.mHint:setVisible(false)
    -- self.mBottomMask:addChild(self.mHint)
    
    -- -- 注册帧事件
    -- self:scheduleUpdate( handler(self, self.updateFrame))
    -- 
end

function StoryModeView:onEnter()

end

function StoryModeView:onExit()

end

function StoryModeView:getMaskHeight()
    return self.mMaskHeight
end

function StoryModeView:updateFrame()
end

function StoryModeView:addTouchMask()
    self:removeTouchMask()
    self.mTouchMaskListener = cc.EventListenerTouchOneByOne:create()
    self.mTouchMaskListener:setSwallowTouches(true)
    self.mTouchMaskListener:registerScriptHandler(function(touch, event)
        return true
    end, cc.Handler.EVENT_TOUCH_BEGAN)

    self.mTouchMaskListener:registerScriptHandler(function(touch, event)

            -- DoEventHandler.overCouldSkipEvent()
        end, cc.Handler.EVENT_TOUCH_ENDED)
    self:getEventDispatcher():addEventListenerWithSceneGraphPriority(self.mTouchMaskListener, self)
    
end

function StoryModeView:removeTouchMask()
    if self.mTouchMaskListener then
        cc.Director:getInstance():getEventDispatcher():removeEventListener(self.mTouchMaskListener)
        self.mTouchMaskListener = nil
    end
end

function StoryModeView:pauseTouchMask()
    if self.mTouchMaskListener then
        self.mTouchMaskListener:setSwallowTouches(false)
    end
end

function StoryModeView:resumeTouchMask()
    if self.mTouchMaskListener then
        self.mTouchMaskListener:setSwallowTouches(true)
    else
        self:addTouchMask()
    end
end

function StoryModeView:showMask(callback,time,hideBorder)
    self:addTouchMask()
    self.mHideBorder = hideBorder
    -- self.mShowTaskBattleConfirm = false
    if hideBorder then
        self.mUpMask:setVisible(false)
        self.mBottomMask:setVisible(false)
    else
        self.mUpMask:setVisible(true)
        self.mBottomMask:setVisible(true)
    end

    
    local function call()
        --self.mHint:setVisible(true)
        if callback then
            callback()
        end
    end
    
    local upAction = cc.MoveTo:create(time,cc.p(0,display.height - self.mMaskHeight))
    local bottomAction = cc.Sequence:create(cc.MoveTo:create(time,cc.p(0,0)),cc.CallFunc:create(call))  
    if self.mPauseMask then

    end
    self.mUpMask:setPosition(cc.p(0,display.height))
    self.mBottomMask:setPosition(cc.p(0,0 - self.mMaskHeight))
    self.mUpMask:stopAllActions()
    self.mBottomMask:stopAllActions()
    self.mUpMask:runAction(upAction)
    self.mBottomMask:runAction(bottomAction)

end

function StoryModeView:hideMask(callback,time)
    
    -- self.mHint:setVisible(false)
    -- if not self.mHideBorder then
    --     self.mUpMask:setVisible(true)
    --     self.mBottomMask:setVisible(true)
    -- end
    self.mPauseMask = false
    self:removeTouchMask()

    
    local function call()
        self.mPauseMask = false
        self:removeTouchMask()
        self.mUpMask:setVisible(false)
        self.mBottomMask:setVisible(false)
        if callback then
            callback()
        end
    end

    self.mUpMask:stopAllActions()
    self.mBottomMask:stopAllActions()

    if time <= 0 then
        self.mUpMask:setPosition(cc.p(0,display.height))    
        self.mBottomMask:setPosition(cc.p(0,0 - self.mMaskHeight))
        call()
    else
        local upAction = cc.Sequence:create(cc.MoveTo:create(time,cc.p(0,display.height)),cc.CallFunc:create(call))
        local bottomAction = cc.Sequence:create(cc.MoveTo:create(time,cc.p(0,0 - self.mMaskHeight)))

        if self.mPauseMask then

        end
        self.mUpMask:setPosition(cc.p(0,display.height - self.mMaskHeight))
        self.mBottomMask:setPosition(cc.p(0,0))
        
        self.mUpMask:runAction(upAction)
        self.mBottomMask:runAction(bottomAction)
    end 
    
end

function StoryModeView:pauseMask()
    self.mPauseMask = true
    self:pauseTouchMask()

    if not self.mHideBorder then
        self.mUpMask:setVisible(true)
        self.mBottomMask:setVisible(true)

        local time = 0.5

        local function callOver()
            self.mUpMask:setVisible(false)
            self.mBottomMask:setVisible(false)
        end
        local upAction = cc.Sequence:create(cc.MoveTo:create(time,cc.p(0,display.height)),cc.CallFunc:create(callOver))
        local bottomAction = cc.Sequence:create(cc.MoveTo:create(time,cc.p(0,0 - self.mMaskHeight)))
        self.mUpMask:stopAllActions()
        self.mBottomMask:stopAllActions()
        self.mUpMask:runAction(upAction)
        self.mBottomMask:runAction(bottomAction)
    end

end

function StoryModeView:resumeMask()
    self.mPauseMask = false
    self:resumeTouchMask()

    if not self.mHideBorder then
        local time = 0.5

        self.mUpMask:setVisible(true)
        self.mBottomMask:setVisible(true)

        local upAction = cc.MoveTo:create(time,cc.p(0,display.height - self.mMaskHeight))
        local bottomAction = cc.Sequence:create(cc.MoveTo:create(time,cc.p(0,0)))  
        self.mUpMask:stopAllActions()
        self.mBottomMask:stopAllActions()
        self.mUpMask:runAction(upAction)
        self.mBottomMask:runAction(bottomAction)
    end
end

function StoryModeView:enter(callback,npcId,time,hideBorder)
    self:showMask(callback,time,hideBorder)
    -- self:cameraZoomIn(npcId)
end

function StoryModeView:exit(callback,npcId,time)
    self:hideMask(callback,time)
    -- self:cameraZoomOut()
end


---
-- npc镜头拉近
--
function StoryModeView:cameraZoomIn(npcId)
--[[
    local focusRole = NpcManager:getNpcById(npcId)

    if focusRole then
        local function callback()

        end

        --MapManager:getMap():transitionFocusToNpc(focusRole,callback)
        MapManager:getMap():setFocusNpcForInit(focusRole)
    else
        focusRole = FieldManager:getMyCharacter()
    end

    if not focusRole then
        return
    end
    
    local scale = 1.5
    local inScreenX = display.width / 2--focusRole:getPositionX() - MapManager:getCameraX()
    local inScreenY = focusRole:getPositionY()

    local scaleInScreenX = inScreenX * scale

    local destX = display.width / 2
    self.mCameraMoveX = (destX - scaleInScreenX)

    local scaleInScreenY = inScreenY * scale
    local destY = focusRole:getPositionY() - MapManager:getMap():getMapOffsetY()
    self.mCameraMoveY = (destY - scaleInScreenY)

    local function actionOver()

    end

    local function forceZoomOver()

    end

    MapManager:forceCameraZoomIn({callback = forceZoomOver})

    MapManager:getMap():cameraZoomTo({
        time = 0.5,
        mx = self.mCameraMoveX,
        my = self.mCameraMoveY,
        scale = scale,
        callback = actionOver})
--]]
end

---
-- npc镜头回归原位
--
function StoryModeView:cameraZoomOut()

    -- if self.mCameraMoveX then

    --     self.mCameraMoveX = 0 - self.mCameraMoveX
    --     self.mCameraMoveY = 0 - self.mCameraMoveY

    --     local function actionOver()
            
    --     end

    --     MapManager:getMap():cameraZoomTo({
    --         time = 0.5,
    --         mx = self.mCameraMoveX,
    --         my = self.mCameraMoveY,
    --         scale = 1.0,
    --         callback = actionOver})

    --     self.mCameraMoveX = 0
    --     self.mCameraMoveY = 0
    -- end

    -- local function actionOver()
            
    -- end

    -- MapManager:getMap():cameraZoomTo({
    --     time = 0.5,
    --     mx = self.mCameraMoveX,
    --     my = self.mCameraMoveY,
    --     scale = 1.0,
    --     callback = actionOver})
end

-- function StoryModeView:showTaskBattleConfirm(okCallback,cancelCallback)
--     self.mShowTaskBattleConfirm = true
--     self.mHint:setVisible(false)
    
    
--     local function removeBtn()
--         self:removeChild(self.mIntoBattle)
--         self.mIntoBattle = nil
--         self:removeChild(self.mCancelTaskBattle)
--         self.mCancelTaskBattle = nil
--     end
    
--     local function ok()
--         okCallback()
--         removeBtn()
--     end
    
--     local function cancel()
--         cancelCallback()
--         removeBtn()
--     end

--     self.mIntoBattle = ccui.Button:create()
--     self.mIntoBattle:setTitleText("进入战斗")
--     self.mIntoBattle:setTitleFontSize(30)
--     self.mIntoBattle:setPosition(cc.p(display.width / 2, self.mMaskHeight / 2))
--     self:addChild(self.mIntoBattle)
--     self.mIntoBattle:addTouchEventListener(function(sender, eventType)
        
--         if eventType == ccui.TouchEventType.began then
            
--         elseif eventType == ccui.TouchEventType.moved then
            
--         elseif eventType == ccui.TouchEventType.ended then
--             ok()
--         else
            
--         end
--     end)

--     self.mCancelTaskBattle = ccui.Button:create()
--     self.mCancelTaskBattle:setTitleText("取消")
--     self.mCancelTaskBattle:setTitleFontSize(30)
--     self.mCancelTaskBattle:setPosition(cc.p(display.width / 2 + display.width / 4, self.mMaskHeight / 2))
--     self:addChild(self.mCancelTaskBattle)
--     self.mCancelTaskBattle:addTouchEventListener(function(sender, eventType)
        
--         if eventType == ccui.TouchEventType.began then
            
--         elseif eventType == ccui.TouchEventType.moved then
            
--         elseif eventType == ccui.TouchEventType.ended then
--             cancel()
--         else
            
--         end
--     end)

        
--     local startTime = os.time()
--     local totalTime = 30
--     local function call()
--         self.mHint:setVisible(false)
            
--         local moveTime = os.time() - startTime
--         local syTime = totalTime - moveTime
--         if syTime <= 0 then
--             cancel()
--         else
--             if self.mCancelTaskBattle then
--                 self.mCancelTaskBattle:setTitleText("取消(" .. syTime .. ")")
--             end
--         end
--     end
        
--     local action = cc.RepeatForever:create(cc.Sequence:create(cc.CallFunc:create(call)))
--     self:runAction(action)
        
-- end

return StoryModeView